package com.filter;

import com.dao.GoodsDao;
import com.model.Goods;

import javax.servlet.*;
import java.io.IOException;

public class GoodsFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String goodsId = servletRequest.getParameter("goodsId");
        GoodsDao goodsDao = new GoodsDao();
        Goods goods = goodsDao.getGoodsByGoodsId(goodsId);
        if (goods == null){
            RequestDispatcher dispatcher = servletRequest.getRequestDispatcher("/error/errorGoods.jsp");
            dispatcher.forward(servletRequest, servletResponse);
        }
        else {
            servletRequest.setAttribute("goods",goods);
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
